package net.wenku8.reader.api

import net.wenku8.reader.entity.ApiResult
import net.wenku8.reader.model.review.ReviewDataList
import net.wenku8.reader.model.review.ReviewDataValue
import net.wenku8.reader.util.parseXml

class ReviewApi {

    companion object {

        fun getReviewList(bookId: String, page : Int = 1): ApiResult<List<ReviewDataValue>> {
            val map = mapOf(
                "action" to "review",
                "do" to "list",
                "aid" to bookId,
                "page" to page.toString(),
            )
            val apiResp = Wenku8Connect.request(map)
            if (!ApiResult.isSuccess(apiResp)){
                return ApiResult.fail(apiResp.message)
            }
            val reviewDataList = parseXml(apiResp.data, ReviewDataList::class.java)
            return ApiResult.success(reviewDataList!!.list)
        }


        fun getReviewContent(reviewId: String, page : Int = 1): ApiResult<List<ReviewDataValue>> {
            val map = mapOf(
                "action" to "review",
                "do" to "show",
                "rid" to reviewId,
                "page" to page.toString(),
            )
            val apiResp = Wenku8Connect.request(map)
            if (!ApiResult.isSuccess(apiResp)){
                return ApiResult.fail(apiResp.message)
            }
            val reviewDataList = parseXml(apiResp.data, ReviewDataList::class.java)
            return ApiResult.success(reviewDataList!!.list)
        }


        fun reviewPost(bookId: String, title: String, content : String): ApiResult<String> {
            val map = mapOf(
                "action" to "review",
                "do" to "post",
                "aid" to bookId,
                "title" to title,
                "content" to content,
            )
            return Wenku8Connect.request(map)
        }

        fun reviewReply(reviewId: String, content : String): ApiResult<String> {
            val map = mapOf(
                "action" to "review",
                "do" to "reply",
                "rid" to reviewId,
                "content" to content,
            )
            return Wenku8Connect.request(map)
        }

    }
}